<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta charset="UTF-8">
    <title>租房委托</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" th:href="@{/favicon.ico}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all"/>
    <style>
        .layui-elem-quote .layui-form .layui-form-item .layui-input-inline {
            width: 150px;
        }
    </style>
</head>
<body class="childrenBody" sec:authorize="hasPermission('/admin/entrust/toLeaseEntrust',null)">
<!-- 查询条件开始 -->
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 80px">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input"  style="width: 150px">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                </div>
            </div>
        </div>
    </form>
</blockquote>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="entrustTable" lay-filter="entrustTable"></table>

    <div id="entrustRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm" sec:authorize="hasPermission('/admin/newHouse/addHouse','house:add')">
            <span class="layui-icon layui-icon-edit"></span>接受委托
        </button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger" sec:authorize="hasPermission('/admin/entrust/deleteEntrust','entrust:delete')">
            <span class="layui-icon layui-icon-delete"></span>删除委托
        </button>
    </div>
</div>

<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv" sec:authorize="hasPermission('/admin/newHouse/addHouse','house:add')">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <input hidden name="saId" value="saId">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" id="title" lay-verify="required" autocomplete="off"
                           placeholder="请输入标题" class="layui-input" maxlength="50" style="width: 400px">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">城市</label>
                <div class="layui-input-inline">
                    <select name="cId" id="cId" lay-search="" lay-verify="required">
                        <option value="">请选择城市</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">楼盘小区</label>
                <div class="layui-input-inline">
                    <input type="text" name="quarters" id="quarters" lay-verify="required" autocomplete="off"
                           placeholder="请输入楼盘小区" class="layui-input" maxlength="40">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="local" id="local" lay-verify="required" autocomplete="off"
                           placeholder="请输入地址" class="layui-input" maxlength="255" style="width: 600px">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">房屋特色</label>
                <div class="layui-input-inline">
                    <input type="text" name="characteristic" id="characteristic" lay-verify="required" autocomplete="off"
                           placeholder="请输入房屋特色(如：朝向西等)" class="layui-input" maxlength="255" style="width: 600px">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 110px">租金(月/元)</label>
                <div class="layui-input-inline">
                    <input type="number" name="price" id="price" lay-verify="required" autocomplete="off"
                           placeholder="请输入租金" class="layui-input" min="0" max="999999999" oninput="checkPrice(this.value)">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 110px">面积(m³)</label>
                <div class="layui-input-inline">
                    <input type="number" name="area" id="area" lay-verify="required" autocomplete="off"
                           placeholder="请输入面积" class="layui-input" min="0" max="999999999" oninput="checkArea(this.value)">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">楼层</label>
                <div class="layui-input-inline">
                    <input type="number" name="higth" id="higth" lay-verify="required" autocomplete="off"
                           placeholder="请输入楼层" class="layui-input" min="0" max="99999" oninput="checkFloor(this.value)">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">总层数</label>
                <div class="layui-input-inline">
                    <input type="number" name="allhigth" id="allhigth" lay-verify="required" autocomplete="off"
                           placeholder="请输入总层数" class="layui-input" min="0" max="99999" oninput="checkTotalFloor(this.value)">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">图片</label>
                <div class="layui-upload-list thumbBox mag0 magt3" id="houseimgDiv" style="width: 200px">
                    <!-- 显示上传的图片 -->
                    <img class="layui-upload-img thumbImg" id="showHouseImg">
                    <!-- 保存当前显示图片的地址 -->
                    <input type="hidden" name="img" id="img" lay-verify="required">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">详细描述</label>
            <div class="layui-input-block">
                <textarea name="details" id="details" placeholder="请输入详细描述" class="layui-textarea"></textarea>            </div>
        </div>
        <input type="hidden" name="type" id="type" value="3">
        <input type="hidden" name="id" value="id">
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit"><span
                        class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" id="dataFrmResetBtn" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<!-- 委托分配角色弹出层  开始 -->
<div style="display: none;padding: 5px" id="selectEntrustRoleDiv">
    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
</div>


<!-- 委托分配角色弹出层  结束 -->


<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript">
    function checkPrice (val) {
        document.getElementById('price').value = val >=0 ? val : 0;
        if(document.getElementById('price').value.length>9)document.getElementById('price').value=document.getElementById('price').value.slice(0,9);
    }
    function checkArea (val) {
        document.getElementById('area').value = val >=0 ? val : 0;
        if(document.getElementById('area').value.length>9)document.getElementById('area').value=document.getElementById('area').value.slice(0,9);
    }
    function checkFloor (val) {
        document.getElementById('floor').value = val >=0 ? val : 0;
        if(document.getElementById('floor').value.length>5)document.getElementById('floor').value=document.getElementById('floor').value.slice(0,5);
        if(! /^\d+$/.test(val))document.getElementById('floor').value = '';
    }
    function checkTotalFloor (val) {
        document.getElementById('totalfloor').value = val >=0 ? val : 0;
        if(document.getElementById('totalfloor').value.length>5)document.getElementById('totalfloor').value=document.getElementById('totalfloor').value.slice(0,5);
        if(! /^\d+$/.test(val))document.getElementById('floor').value = '';
    }
</script>
<script type="text/javascript">
    var tableIns;
    layui.use(['jquery', 'form', 'table', 'layer','upload'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var upload=layui.upload;

        //加载 数据
        tableIns = table.render({
            elem: '#entrustTable'
            , url: '/admin/entrust/queryLeaEntrust'
            , toolbar: '#entrustToolBar' //开启头部工具栏，并为其绑定左侧模板
            , title: '委托数据表'
            , page: true
            , cols: [ [
                {field: 'name', title: '姓名', align: 'center', width: '150'}
                , {field: 'phone', title: '手机号', align: 'center', width: '130'}
                , {field: 'cName', title: '所属城市', align: 'center', width: '130'}
                , {field: 'local', title: '地址', align: 'center', width: '365'}
                , {field: 'time', title: '委托时间', align: 'center', width: '200'}
                , {fixed: 'right', title: '操作', toolbar: '#entrustRowBar', align: 'center', width: '230'}
            ] ]
        });

        //模糊查询
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监听行工具条的事件
        table.on("tool(entrustTable)", function (obj) {
            var data = obj.data; //获得当前行数据
            switch (obj.event) {
                case 'update':
                    openUpdateEntrustLayer(data);
                    break;
                case 'delete':
                    deleteEntrust(data);
                    break;
            };
        });

        var mainIndex;
        var url;

        //打开修改的弹出层
        function openUpdateEntrustLayer(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                title: '接受委托',
                success: function () {
                    $("#dataFrm")[0].reset();
                    $("#showHouseImg").attr("src","")
                    $("#img").val("")
                    $("#customerID").val(data.cId);
                    //装载新的数据
                    form.val("dataFrm", data);
                    var cId = data.cId;
                    //城市
                    $.ajax({
                        url: "/admin/city/getCity1",
                        success: function (res){
                            var citys = res.data;
                            var dom_mgr=$("#cId");
                            var html = "<option value=''>请选择城市</option>";
                            $.each(citys, function (index, item) {
                                html += "<option value='" + item.id + "'>" + item.name + "</option>";
                            });
                            $("#cId").html(html);
                            //选中一个
                            dom_mgr.val(cId);
                            //重新渲染
                            form.render("select");
                        },
                        dataType: "json"
                    });
                    url = "/admin/house/addHouse";
                }
            });
        }
        form.on("submit(doSubmit)", function (data) {
            // layer.msg(JSON.stringify(data.field));
            $.post(url, data.field, function (res) {
                if (res.code == 200) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
            return false;
        })

        //删除
        function deleteEntrust(data) {
            layer.confirm('你确定要删除这个委托吗?', {icon: 3, title: '提示'}, function (index) {
                // alert(data.id);
                $.post("/admin/entrust/deleteEntrust", {id: data.id}, function (res) {
                    if (res.code == 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

        //上传图片
        //上传缩略图
        upload.render({
            elem: '#houseimgDiv',
            url: '/admin/file/uploadFile',
            method : "post",
            acceptMime:'image/*',
            field:"mf",
            done: function(res, index, upload){
                $('#showHouseImg').attr('src',""+"/admin/file/downloadShowFile?path="+res.data.src);
                $('#img').val(res.data.src);
                $('#houseimgDiv').css("background","#fff");
            }
        });
    });
</script>

</body>
</html>